import sys, os
from PyQt4 import QtGui, QtCore
from box import *
from window import *
from enum_classes import *
from DSLParser import *
from questionMark_solver import *


main_dict = None
       
def run_code(caller,event_name = None):
   user_code = main_dict["main_window"].get_Gui_Var("textbox_text")
   f = open('temp_dsl_file.txt', 'w')
   f.write(user_code)
   f.close()
   os.system("dsl_runner.py")

example_dict = {
   0:"""Select one of the examples from the combo box and press "run!" """,

   1:"""b <- (button:100x?)[text="Hello"]
l <- (label:?x100)[text="world",halign="center", valign="middle"]

main_window<- (b|l :200x200)[text="Hello world!"] """,

   3:"""b<- (button:?x?)[text="@i"]
b2<-(button:?x?)[text="@i"]
top<-{b |* i=1..10}
bottom<-{b2 |* i=["one","two","three"]}

main_window <- (top --- bottom :300x300) """,

   4:"""b<- (button:?x?)[text="@i"]
b2<-(button:?x?)[text="@i"]
top<-{b ---* i=1..10}
bottom<-{b2 ---* i=["one","two","three"]}

main_window <- (top | bottom :300x300)[text="loop"]

 """,

   5:"""b<- (button:?x?)[text="@i:@j"]
row<- {b |* i=1..9 }
array <- {row ---* j=1..9 }

main_window <- (array :400x400)[text="2D loop"]
 """,

   6:"""b<- (button: 20x20)[]
small_row<-{b |*x=1..5}
small_box <- {small_row ---* y=1..5}
with_fillers<- (:?x?)[] --- (:?x?)[] | small_box | (:?x?)[] --- (:?x?)[]

big_row <- { with_fillers |* z=1..5 }
big_box <- {big_row ---* w=1..5 }

main_window <- (big_box : 600x600)[text="4D loop"]
 """,

   8:"""
f(a,b,c){
2*d = a+b+c
}

x = 100
but1 <- (button:(f(x,x,x))x(f(150,150,150)))[text="hi!"]

main_window <- ( (:?x?)[]---(:?x?)[]  |   but1  | (:?x?)[] --- (:?x?)[] :400x400)[text="vars and functions"]
""",

   2:"""
label_widget <- (label:?x?)[text = "A Label Widget" , halign = "center" , valign = "middle" , bgcolor = "5F9F9F" , fgcolor = "8A2BE2"]

textbox_widget <- (textbox:?x30)[text = "A Textbox Widget" , bgcolor = "5F9F9F" , fgcolor = "8A2BE2"]

button_widget <- (button:90x30)[text = "A button Widget", bgcolor = "5F9F9F" , fgcolor = "8A2BE2"]

checkBox_widget <- (checkbox:?x?)[checked = "0"]

radioButton_widget <- {  (radiobutton:?x?)[text = "@i"] |* i=["A" , "B" ,"C"] }

image_widget <- (:450x300)[image = "pictures/CompanionCube.png"]

frame_widget <- (:?x30)[style = "True"]

scrollArea_widget <- ( (label:450x300)[text = "A ScrollArea", valign = "top" , halign = "left"] | (:0x0)[] :450x100)

splitter_widget <- ( 	(label:?x?)[bgcolor_var = "color1" , bgcolor = "3299CC" , text = "Left" , text_var = "lab1_text" , halign = "left" , valign = "top"] |
	(label:?x?)[bgcolor_var = "color2" , bgcolor = "8DEEEE" , text = "Right" , text_var = "lab2_text" , halign = "left" , valign = "top"] 
	:450x100 )[onsplit="split"]

body <- (  	(:?x?)[] ---  label_widget --- (:?x?)[] --- textbox_widget --- (:?x?)[] --- button_widget --- (:?x?)[] --- checkBox_widget --- (:?x?)[] --- 
	radioButton_widget --- (:?x?)[] --- image_widget --- (:?x?)[] --- frame_widget --- (:?x?)[] --- scrollArea_widget --- (:?x?)[] --- 
	splitter_widget --- (:?x?)[] )

main_window <- (  (:?x?)[] | body | (:?x?)[] :500x800)
""",

   7:"""
picture <- (:?x?)[image = "pictures/stevejobs.png"]

picture_partial_frame <- (	(label:?x1)[ bgcolor = "000000"] --- (label:?x5)[ bgcolor = "9ACD32"] ---
		picture ---  (label:?x5)[ bgcolor = "9ACD32"] --- (label:?x1)[ bgcolor = "000000"]
 )

picture_frame <- ( (label:1x?)[ bgcolor = "000000"] | (label:5x?)[ bgcolor = "9ACD32"] | picture_partial_frame |
		(label:5x?)[ bgcolor = "9ACD32"] | (label:1x?)[ bgcolor = "000000"] 
:150x?)

apple_pic <- (:?x?)[image = "pictures/apple.png"]

flag_pic <- (:?x?)[image = "pictures/flag.png"]

apple_flag <- (apple_pic | (label:?x?)[bgcolor = "ffffff"] | flag_pic :?x60)

profile <- ( 	(label:?x1)[ bgcolor = "000000"] --- (label:?x?)[text = "   Profile", halign = "left"] 
	--- (label:?x?)[text = "   Steve Jobs", halign = "left", bgcolor = "ffffff"] --- (label:?x?)[text = "   CEO", halign = "left", bgcolor = "ffffff"] 
	--- (label:?x1)[bgcolor = "000000"] --- (label:?x?)[text = "   Apple Corporation", halign = "left", bgcolor = "ffffff"] 
	--- (label:?x1)[bgcolor = "000000"] --- apple_flag --- (label:?x1)[ bgcolor = "000000"]
)

picture_and_profile <- ( picture_frame | profile | (label:1x?)[ bgcolor = "000000"] :?x200 )


item1 <- (  (:?x?)[image = "pictures/macbookair.png"] --- (label:?x30)[text = "MacBook Air", halign = "center" , valign = "middle", bgcolor = "ffffff"]  )
item2 <- (  (:?x?)[image = "pictures/ipod.png"] --- (label:?x30)[text = "iPod", halign = "center" , valign = "middle", bgcolor = "ffffff"]  )
item3 <- (  (:?x?)[image = "pictures/ipad.png"] --- (label:?x30)[text = "iPad", halign = "center" , valign = "middle", bgcolor = "ffffff"]  )
item4 <- (  (:?x?)[image = "pictures/humancentipad.png"] --- (label:?x30)[text = "HumancentiPad", halign = "center" , valign = "middle", bgcolor = "ffffff"]  )

items_list <- (	 (label:20x?)[bgcolor = "ffffff"] | item1 | (label:20x?)[bgcolor = "ffffff"] |  item2 |  (label:20x?)[bgcolor = "ffffff"] |
	 item3 | (label:20x?)[bgcolor = "ffffff"] | item4  |  (label:20x?)[bgcolor = "ffffff"]
:?x?)

projects_body <- (	
	(label:?x1)[bgcolor = "000000"] --- (label:?x30)[text = "   Projects"] --- (label:?x?)[bgcolor = "ffffff"] ---  items_list ---
	(label:?x?)[bgcolor = "ffffff"] ---  (label:?x1)[bgcolor = "000000"]
) 

projects <- (   (label:1x?)[bgcolor = "000000"] | projects_body | (label:1x?)[bgcolor = "000000"]  )


my_team <- (  	(label:?x1)[bgcolor = "000000"] --- (label:?x30)[text = "My Team" , halign = "center" , valign = "middle", bgcolor = "7A67EE" , fgcolor = "ffffff"]
	--- (label:?x1)[bgcolor = "000000"] --- (label:?x?)[bgcolor = "ffffff"] --- (label:?x1)[bgcolor = "000000"]
:180x?)

pic_proj_prof <- ( picture_and_profile --- projects)

body <- ( (label:1x?)[bgcolor = "000000"] | my_team | pic_proj_prof )

main_window <- ( body :600x400)[text = "Manager Tycoon Pro"]
""",

9:"""
top_label <- (label:?x40)[text="Enter your DSL code here: (or select an example from the combo box below)"]

text_edit <- (textbox:?x?)[text_var="textbox_text"]

run_button <- (button:?x?)[text="Run!", onclick="run_clicked"]

example_box <- (combobox:?x?)[data=["Choose one:","Hello, world!", "widgets" , "Loop", "V-Loop", "2D Loop", "4D Loop", "Steve Jobs" , "Vars & Funcs" , "dsl_tester" ], combobox_var = "selected_example", 
 onselect = "combo_changed"]

bottom_row <- ( run_button|example_box :?x30)

main_window <- (top_label --- text_edit --- bottom_row :800x600)
"""
   }

def select_example(caller,event_name=None):
   selected_val = main_dict["main_window"].get_Gui_Var("selected_example")
   main_dict["main_window"].change_var("textbox_text",example_dict[selected_val])

if __name__ == '__main__':
   app = QtGui.QApplication(sys.argv)
   box_dict = parse("dsl_tester.txt")
   main_dict = solve(box_dict[0])
   main_dict["main_window"].bind("run_clicked",run_code)
   main_dict["main_window"].bind("combo_changed",select_example)
   main_dict["main_window"]({}) 
   sys.exit(app.exec_())

